package com.maoye.ossp.biz.statement.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.api.query.QueryOP;
import com.maoye.ossp.biz.statement.entity.StatementSaleNote;
import com.maoye.ossp.biz.statement.entity.StatementSales;
import com.maoye.ossp.biz.statement.service.StatementSaleNoteService;
import com.maoye.ossp.biz.statement.service.StatementSalesService;
import com.maoye.ossp.biz.statement.util.BizStatementConstant;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;

/**
 * 销售对账单controller类
 * @author yanghao
 *
 */
@Controller
@RequestMapping(value = "biz/statement/statementSales/")
public class StatementSalesController  extends BaseController{
	
	@Resource
	private StatementSalesService statementSalesService;
	@Resource
	private StatementSaleNoteService statementSaleNoteService;
	
	/**
	 * 对账单列表(默认展示所有对账单)
	 * @param statementSales
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "list")
	public String list(StatementSales statementSales, HttpServletRequest request,Model model) {
		QueryFilter queryFilter = getQuerFilter(request, statementSales);
		Page<StatementSales> page = statementSalesService.find(queryFilter);
		model.addAttribute("page", page);
		return resultPage("/biz/statement/statementSales/list",model,request);
	}
	/**
	 * 根据条件查询对账单列表
	 * @param statementSales
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "queryByTerm")
	public String queryByTerm(StatementSales statementSales, HttpServletRequest request, Model model) {
		QueryFilter queryFilter = getQuerFilter(request, statementSales);
		queryFilter.addFilter("saleDate", statementSales.getSaleDate(), QueryOP.EQUAL);
		queryFilter.addFilter("isReconciliation", statementSales.getIsReconciliation(), QueryOP.EQUAL);
		queryFilter.addFilter("supplierNo", statementSales.getSupplierNo(), QueryOP.LIKE);
		Page<StatementSales> page = statementSalesService.find(queryFilter);
		model.addAttribute("page", page);
		return resultPage("/biz/statement/statementSales/list",model,request);
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "view")
	public String view(StatementSales statementSales,HttpServletRequest request, Model model) {
		StatementSales sales = statementSalesService.get(statementSales.getId().toString());
		Map map = new HashMap();
		Date date = sales.getSaleDate();
		 DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
		 String saleDate =format2.format(date); 
	    map.put("saleDate",saleDate);
	    map.put("supplierNo", sales.getSupplierNo());
	    List<StatementSaleNote> list = statementSaleNoteService.getSaleNotes(map);
	    model.addAttribute("list", list);
		model.addAttribute("statementSales", sales);
		return resultPage("/biz/statement/statementSales/view",model,request);
	}
	
	/**
	 * ajax确认对账
	 * @param request
	 * @param redirectAttributes
	 * @return
	 * @throws IOException 
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "reconciliation")
	public void reconciliation(StatementSales statementSales,HttpServletRequest request,HttpServletResponse response,
			RedirectAttributes redirectAttributes) throws IOException{
		StringBuilder jsonStr = new StringBuilder();
		response.setCharacterEncoding("utf-8");
		String contextPath=request.getContextPath();
		PrintWriter out = response.getWriter();
		try {
			Map map = new HashMap();
			map.put("id", statementSales.getId());
			map.put("isReconciliation",BizStatementConstant.ISRECONCILIATION);
			int count = statementSalesService.reconciliation(map);
			if(count >0){
				jsonStr.append("{success:true}");
			}else{
				jsonStr.append("{success:false}");
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			jsonStr.append("{success:false}");
		}
		out.print(jsonStr.toString());
		out.flush();
		out.close();
	}
	
	public StatementSalesService getStatementSalesService() {
		return statementSalesService;
	}
	public void setStatementSalesService(StatementSalesService statementSalesService) {
		this.statementSalesService = statementSalesService;
	}
}
